<?php
class ContactController extends Apps_Controller_Action_Default {

	public function indexAction() {
        $rq = $this->_request;
        $f  = new Form_Contact();

        $tbSiteSetting = new Model_DbTable_SiteSetting();
        $siteSetting = $tbSiteSetting->getSiteSetting()->query()->fetch();

        if($rq->isPost()) {
            if($f->isValid($rq->getParams())) {


                $fullName = $f->getValue('full_name');
                $email    = $f->getValue('email');
                $phone    = $f->getValue('phone');
                $subject  = $f->getValue('subject');
                $message  = $f->getValue('message');
                $captcha  = $f->getValue('captcha');

                if($email != '' && $fullName != '') {
                    //check captcha
                    $captchaSession = new Zend_Session_Namespace('captchaSession');
                    $captchaCodeSession = $captchaSession->captcha_code;

                    $captchaCode = md5($captcha . '760211');

                    if($captchaCodeSession != $captchaCode) {
                        $this->view->error = "Captcha_doesn't_match";
                    } else {
                        $data = array(
                                'full_name' => $this->quote($fullName),
                                'email'     => $this->quote($email),
                                'phone'     => $this->quote($phone),
                                'subject'   => $this->quote($subject),
                                'message'   => $this->quote($message)
                                );

                        $tbContact = new Model_DbTable_Contact();

                        $id = $tbContact->insert($data);
                        if($id) {
                            $this->_flashMessenger->addMessage($siteSetting['reply_message_contact']
                                    ? $siteSetting['reply_message_contact']
                                    : 'Cảm ơn bạn đã liên hệ đến chúng tôi!');

                            $this->_redirect('/Lien-He.html');
                        } else {
                            $this->view->error = "Error!";
                        }
                    }
                }
            }
        }
        $this->view->form = $f;

        $tbSiteInfo = new Model_DbTable_SiteInfo();
        $this->view->siteInfo = $tbSiteInfo->getSiteInfo()->query()->fetch();

        $this->view->siteSetting = $siteSetting;
	}
}